package com.yuantu.judiciary.controller;

import com.yuantu.judiciary.annotation.Log;
import com.yuantu.judiciary.common.Response;
import com.yuantu.judiciary.common.ResponseFactory;
import com.yuantu.judiciary.model.dto.SearchRecordContentDTO;
import com.yuantu.judiciary.model.enums.SearchType;
import com.yuantu.judiciary.model.form.AddQueryRecordForm;
import com.yuantu.judiciary.model.form.GetQueryInfoForm;
import com.yuantu.judiciary.model.vo.*;
import com.yuantu.judiciary.service.IQueryRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 查询记录模块
 *
 * @author syw
 * @since 2023-07-06
 **/
@RestController
@RequestMapping(value = "/query-record")
@Api(tags = "查询记录模块")
@RequiredArgsConstructor
public class QueryRecordController extends BaseController {

    private final IQueryRecordService queryRecordService;

    @ApiOperation(value = "根据内容获取查询记录-个人尽调")
    @PostMapping("/get/person")
    public Response<QueryRecordVO> getPersonRecord(@RequestBody SearchRecordContentDTO form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(queryRecordService.get(userId, form, SearchType.PERSON));
    }


    @ApiOperation(value = "查取他人记录")
    @PostMapping("/get/other")
    public Response<String> getPersonRecord(@RequestBody SearchRecordContentDTO form, @RequestParam Long userId) {
        return ResponseFactory.okResponse(queryRecordService.getOther(userId, form, SearchType.PERSON));
    }


    @ApiOperation(value = "根据内容获取查询记录-企业尽调")
    @PostMapping("/get/enterprise")
    public Response<QueryRecordVO> getEnterpriseRecord(@RequestBody SearchRecordContentDTO form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(queryRecordService.get(userId, form, SearchType.ENTERPRISE));
    }

    @ApiOperation(value = "根据内容获取查询记录-查自己（支付宝小程序）")
    @GetMapping("/get/person/by-self")
    public Response<Long> getPersonRecordBySelf() {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(queryRecordService.getBySelf(accountId, SearchType.PERSON));
    }


    //todo 查个人
    @ApiOperation(value = "获取查询记录-个人尽调")
    @PostMapping("/search/person/page")
    public Response<PageVO<QueryRecordPersonVO>> searchPersonPage(@RequestParam(required = false) String type,
                                                                  @PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(queryRecordService.searchPersonPage(type, userId, pageable));
    }

    @ApiOperation(value = "获取查询记录-个人尽调(增加个人/他人筛选)")
    @PostMapping("/search/person/conditionPage")
    public Response<PageVO<QueryRecordPersonVO>> searchConditionPersonPage(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable,
                                                                           @RequestParam(required = false) Integer type) {
        Long userId = getLoginAccount().getAccountId();
        if (type == null) {
            return ResponseFactory.okResponse(queryRecordService.searchPersonPageCondition(userId, pageable, null));
        } else {
            if (type == 1) {
                return ResponseFactory.okResponse(queryRecordService.searchPersonPageCondition(userId, pageable, Boolean.TRUE));
            } else {
                return ResponseFactory.okResponse(queryRecordService.searchPersonPageCondition(userId, pageable, Boolean.FALSE));
            }
        }
    }


    //todo 查企业
    @ApiOperation(value = "获取查询记录-企业尽调")
    @PostMapping("/search/enterprise/page")
    public Response<PageVO<QueryRecordEnterpriseVO>> searchEnterprisePage(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(queryRecordService.searchEnterprisePage(userId, pageable));
    }

    @ApiOperation(value = "我的查询-个人尽调")
    @GetMapping("/search/person/page/my")
    public Response<List<MySearchPageVO>> getMySearchPersonPage(@RequestParam(required = false) String type) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(queryRecordService.getMySearchPage(userId, SearchType.PERSON, type));
    }

    @ApiOperation(value = "我的查询-企业尽调")
    @GetMapping("/search/enterprise/page/my")
    public Response<List<MySearchPageVO>> getMySearchEnterprisePage() {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(queryRecordService.getMySearchPage(userId, SearchType.ENTERPRISE, null));
    }

    //todo 获取最新数据
    @ApiOperation(value = "获取最新数据")
    @PostMapping("/get/latest")
    public Response<Boolean> getLatestData(@RequestBody @Validated GetQueryInfoForm form) {
        Long userId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(queryRecordService.getLatestData(userId, form));
    }

    @ApiOperation(value = "获取套餐使用记录")
    @GetMapping("/get/list")
    public Response<PageVO<QueryRecordInfoVO>> getList(@PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(queryRecordService.getList(accountId, pageable));
    }

    @ApiOperation(value = "添加查询记录（企业用户分享二维码）")
    @PostMapping("/add/by-share")
    public Response<Boolean> addRecordByShare(@RequestBody @Validated AddQueryRecordForm form) {
        form.setType(SearchType.PERSON);
        return ResponseFactory.okResponse(queryRecordService.addRecordByShare(form));
    }

    @ApiOperation(value = "获取最近查询自己的记录")
    @GetMapping("/get/latestSelfQuery")
    public Response<List<LatestQueryVO>> getLatestSelfQuery() {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(queryRecordService.getLatestQueryVO(accountId, SearchType.PERSON, Boolean.TRUE));
    }

    @ApiOperation(value = "获取最近查询他人记录")
    @GetMapping("/get/latestOtherPersonQuery")
    public Response<List<LatestQueryVO>> getLatestOtherPersonQuery() {

        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(queryRecordService.getLatestQueryVO(accountId, SearchType.PERSON, Boolean.FALSE));
    }

    @ApiOperation(value = "获取最近查询公司记录")
    @GetMapping("/get/latestCompanyQuery")
    public Response<List<LatestQueryVO>> getLatestOtherCompanyQuery() {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(queryRecordService.getLatestQueryVO(accountId, SearchType.ENTERPRISE, Boolean.FALSE));
    }



    @ApiOperation(value = "授权书上传查询列表 isUpload 0 未上传 1 已上传 queryType PERSON 查个人 ENTERPRISE 查企业")
    @GetMapping("/auth")
    public Response<List<QueryRecordAuthVO>> getQueryRecordAuthList(@RequestParam Integer isUpload, @RequestParam String queryType) {
        Long accountId = getLoginAccount().getAccountId();
        return ResponseFactory.okResponse(queryRecordService.getQueryRecordAuthList(accountId, isUpload, queryType));
    }



    @ApiOperation(value = "管理端-查询记录列表")
    @PostMapping("/list")
    public Response<PageVO<QueryDetailVO>> searchQueryRecordList(@RequestBody QuerySearchVO querySearch,
                                                                  @PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable) {
        return ResponseFactory.okResponse(queryRecordService.searchQueryRecordList(querySearch, pageable));
    }


    @ApiOperation(value = "管理端-查询时间详情")
    @GetMapping("/time")
    public Response<List<QueryTimeVO>> searchQueryTimeInfos(@RequestParam Long queryId) {
        return ResponseFactory.okResponse(queryRecordService.searchQueryTimeInfos(queryId));
    }


    @ApiOperation(value = "管理端-查询记录授权管理列表")
    @PostMapping("/auth/list")
    public Response<PageVO<QuerySourceVO>> searchQueryRecordAuthList(@RequestBody QuerySourceSearchVO querySourceSearch,
                                                                 @PageableDefault(sort = "createTime", direction = Sort.Direction.DESC) Pageable pageable) {
        return ResponseFactory.okResponse(queryRecordService.searchQueryRecordAuthList(querySourceSearch, pageable));
    }


    @Log(title = "新增查询记录授权")
    @ApiOperation(value = "管理端-新增查询记录授权")
    @GetMapping("/auth/add")
    public Response<Boolean> addQueryRecordAuth(@RequestParam Long queryId) {
        return ResponseFactory.okResponse(queryRecordService.addQueryRecordAuth(queryId));
    }






}
